package com.information.manage.basic.common.recycling;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecoveryDataOperateManager {

    //依赖关系
    /**
     * key: 操作器绑定的实体类
     * values: 依赖key的实体类对应的操作器集合
     */
    private Map<Class, List<RecoveryDataOperator>> relationshipMap = new HashMap<>();

    //操作对象关系
    private Map<Class, RecoveryDataOperator> dataOperateMap = new HashMap<>();

    private static class Communal implements RecoveryDataOperator {

        RecoveryDataOperateManager manager;

        RecoveryDataOperator dataOperate;

        public Communal(RecoveryDataOperateManager manager, RecoveryDataOperator dataOperate) {
            this.manager = manager;
            this.dataOperate = dataOperate;
        }

        @Override
        public void deleteData(Class entityClass, List<Integer> ids) {
            List<RecoveryDataOperator> dataOperates = manager.relationshipMap.get(entityClass);
            //当不存在下级时  删除自身
            if (dataOperates == null){
                this.dataOperate.deleteData(entityClass, ids);
                return;
            }
            //执行删除操作
            for (RecoveryDataOperator operator : dataOperates) {
                try{
                    ((Communal) operator).dataOperate.deleteData(entityClass, ids);
//                    operator.deleteData(entityClass, ids);
                }catch (Exception e){
                    throw new RecoveryException(entityClass.getName() + "删除失败", e);
                }
            }
            //下级全部执行成功后 删除自身
            this.dataOperate.deleteData(entityClass, ids);
        }

        @Override
        public Class getEntityClass() {
            return dataOperate.getEntityClass();
        }

        @Override
        public List<Class> dependencies() {
            return this.dataOperate.dependencies();
        }

    }

    public void addDataOperate(RecoveryDataOperator dataOperator){
        Communal communal = new Communal(this, dataOperator);
        Class clazz = dataOperator.getEntityClass();
        //处理依赖关系
        if (dataOperator.dependencies() == null) throw new RuntimeException("依赖关系不能为null值, 请使用Collections.EMPTY_LIST");
        List<Class> dependencies = dataOperator.dependencies();
        for (Class aClass : dependencies) {
            List<RecoveryDataOperator> operators = relationshipMap.get(aClass);
            if(operators == null){
                operators = new ArrayList<>();
                relationshipMap.put(aClass, operators);
                operators.add(communal);
            }else{
                // 添加回收处理器
                for (RecoveryDataOperator operator : operators) {  //value中存在该依赖关系时直接跳过
                    if (operator.getEntityClass() == clazz) continue;
                    //key存在 value不存在该依赖关系时追加
                    operators.add(communal);
                    break;
                }
            }
        }
   //     dataOperateMap.put(dataOperator.getClass(), communal);
        dataOperateMap.put(clazz, communal);
    }

    public void deleteData(Class entityClass, List<Integer> ids) {
        RecoveryDataOperator dataOperator = dataOperateMap.get(entityClass);
        if(dataOperator == null){
            throw new RecoveryException(entityClass.getName() + "没有回收操作器");
        }
        dataOperator.deleteData(entityClass, ids);
    }

}
